/**
 * 
 */
package com.nari.baseinfo.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.baseinfo.model.IHeatMeter;
import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.heatshare.model.IAssignMeter;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.platform.utils.StringUtils;
import com.nari.rescontroller.dao.IResourceAccessControlDao;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class HeatMeterDaoImpl extends GenericDaoImpl<IHeatMeter, String> implements IHeatMeterDao {
	private IResourceAccessControlDao iResourceAccessControlDao;
	
	/**
	 * 分页查询热量表数据
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param meterNo 热量表表号(可选)
	 * @param meterType 热表型号(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-26
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatMeter> queryHeatMeterPage(String projectUID, String communityUID, 
			String meterNo, String meterType, PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
		//所属项目
		if(projectUID!=null && !"".equals(projectUID.trim())) {
			listCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
			countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		//所属小区
		if(communityUID!=null && !"".equals(communityUID.trim())) {
			listCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
			countCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
		}
		//热量表表号
		if(meterNo!=null && !"".equals(meterNo.trim())) {
			listCriteria.add(Restrictions.ilike("meterNo", meterNo.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("meterNo", meterNo.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//热表型号
		if(meterType!=null && !"".equals(meterType.trim())) {
			listCriteria.add(Restrictions.ilike("meterType", meterType.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("meterType", meterType.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
		if(!"".equals(authorityRes)) {
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
		}
		//统计结果集个数
		int total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		//结果集排序
		listCriteria.addOrder(Order.asc("meterNo")).addOrder(Order.asc("meterType"));
		//结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatMeter> meterPage = new ResultWithPageCond<IHeatMeter>();
		meterPage.setList(listCriteria.list());
		meterPage.setPageCond(pageCond);
		return meterPage;
	}
	
	/**
	 * 通过热量表表号查询热量表信息
	 * @param meterNo
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-30
	 */
	public IHeatMeter queryMeterByMeterNo(String meterNo) throws Exception {
		String hsql = "from IHeatMeter hm " +
				"where hm.meterNo='"+meterNo+"'";
		List<IHeatMeter> meterList = this.runQueryHSql(hsql);
		IHeatMeter heatMeter = null;
		if(meterList!=null && meterList.size()>0) {
			heatMeter = meterList.get(0);
		}
		return heatMeter;
	}
	
	/**
	 * 通过热量表所属关系查询热量表列表
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param shareAreaUID 所属分摊域(可选)
	 * @return 若所属关系均不存在，则返回空列表
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-30
	 */
	@SuppressWarnings("unchecked")
	public List<IHeatMeter> queryMetersByBelongs(String projectUID, String communityUID, 
			String buildingUID, String shareAreaUID) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
		if((projectUID!=null && !"".equals(projectUID.trim())) 
				|| (communityUID!=null && !"".equals(communityUID.trim()))
				|| (buildingUID!=null && !"".equals(buildingUID.trim()))
				|| (shareAreaUID!=null && !"".equals(shareAreaUID.trim()))) {
			//所属项目
			if(projectUID!=null && !"".equals(projectUID.trim())) {
				listCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
			}
			//所属小区
			if(communityUID!=null && !"".equals(communityUID.trim())) {
				listCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
			}
			//所属楼栋
			if(buildingUID!=null && !"".equals(buildingUID.trim())) {
				listCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
			}
			//所属分摊域
			if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
				listCriteria.add(Restrictions.eq("shareAreaID", shareAreaUID.trim()));
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
			if(!"".equals(authorityRes)) {
				//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
				listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
			}
			//结果集排序
			listCriteria.addOrder(Order.asc("meterNo")).addOrder(Order.asc("meterType"));
			return listCriteria.list();
		}
		return new ArrayList<IHeatMeter>();
	}
	
	/**
	 * 通过热量表所属关系统计热量表个数
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param shareAreaUID 所属分摊域(可选)
	 * @return 
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-30
	 */
	public long countMeterNumberByBelongs(String projectUID, String communityUID, 
			String buildingUID, String shareAreaUID) throws Exception {
		Criteria countCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
		if(projectUID!=null && !"".equals(projectUID.trim())) {
			countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		//所属小区
		if(communityUID!=null && !"".equals(communityUID.trim())) {
			countCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
		}
		//所属楼栋
		if(buildingUID!=null && !"".equals(buildingUID.trim())) {
			countCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
		}
		//所属分摊域
		if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
			countCriteria.add(Restrictions.eq("shareAreaID", shareAreaUID.trim()));
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
		if(!"".equals(authorityRes)) {
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
		}
		//统计结果集个数
		long total = (Long) countCriteria.setProjection(Projections.rowCount()).list().get(0);
		return total;
	}
	
	/**
	 * 根据所属分摊域信息查询热量表信息
	 * @param shareAreaUID 所属分摊域(必选)
	 * 说明：若分摊域ID不存在，则返回零结果集
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param meterNo 热量表表号(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatMeter> queryHeatMeterPageByAreaUID(String shareAreaUID, 
			String projectUID, String communityUID, String meterNo, 
			PageCond pageCond) throws Exception {
		List<IHeatMeter> heatMeterList = new ArrayList<IHeatMeter>();
		int total = 0;
		//分摊域存在时，分页查询热量表列表
		if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
			Criteria listCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
			Criteria countCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
			//所属分摊域
			listCriteria.add(Restrictions.eq("shareAreaID", shareAreaUID.trim()));
			countCriteria.add(Restrictions.eq("shareAreaID", shareAreaUID.trim()));
			//所属项目
			if(projectUID!=null && !"".equals(projectUID.trim())) {
				listCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
				countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
			}
			//所属小区
			if(communityUID!=null && !"".equals(communityUID.trim())) {
				listCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
				countCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
			}
			//热量表表号
			if(meterNo!=null && !"".equals(meterNo.trim())) {
				listCriteria.add(Restrictions.ilike("meterNo", meterNo.trim().toLowerCase(), MatchMode.ANYWHERE));
				countCriteria.add(Restrictions.ilike("meterNo", meterNo.trim().toLowerCase(), MatchMode.ANYWHERE));
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
			if(!"".equals(authorityRes)) {
				//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
				listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
				countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
			}
			//统计结果集个数
			total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
			//结果集排序
			listCriteria.addOrder(Order.asc("meterNo")).addOrder(Order.asc("meterType"));
			//结果集分页
			listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
			heatMeterList = listCriteria.list();
		}
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatMeter> meterPage = new ResultWithPageCond<IHeatMeter>();
		meterPage.setList(heatMeterList);
		meterPage.setPageCond(pageCond);
		return meterPage;
	}
	
	/**
	 * 查询不隶属于某个分摊域下的热量表列表
	 * @param shareAreaUID 所属分摊域(必选)
	 * 说明：若分摊域ID不存在，则返回零结果集
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param meterNo 热量表表号(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatMeter> queryMeterPageExceptAreaUID(String shareAreaUID, 
			String projectUID, String communityUID, String meterNo, 
			PageCond pageCond) throws Exception {
		List<IHeatMeter> heatMeterList = new ArrayList<IHeatMeter>();
		int total = 0;
		//分摊域存在时，分页查询热量表列表
		if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
			Criteria listCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
			Criteria countCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
			//不隶属于某个分摊域
			listCriteria.add(Restrictions.or(Restrictions.isNull("shareAreaID"), Restrictions.eq("shareAreaID", "")));
			countCriteria.add(Restrictions.or(Restrictions.isNull("shareAreaID"), Restrictions.eq("shareAreaID", "")));
			//所属项目
			if(projectUID!=null && !"".equals(projectUID.trim())) {
				listCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
				countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
			}
			//所属小区
			if(communityUID!=null && !"".equals(communityUID.trim())) {
				listCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
				countCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
			}
			//热量表表号
			if(meterNo!=null && !"".equals(meterNo.trim())) {
				listCriteria.add(Restrictions.ilike("meterNo", meterNo.trim().toLowerCase(), MatchMode.ANYWHERE));
				countCriteria.add(Restrictions.ilike("meterNo", meterNo.trim().toLowerCase(), MatchMode.ANYWHERE));
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
			if(!"".equals(authorityRes)) {
				//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
				listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
				countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
			}
			//统计结果集个数
			total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
			//结果集排序
			listCriteria.addOrder(Order.asc("meterNo")).addOrder(Order.asc("meterType"));
			//结果集分页
			listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
			heatMeterList = listCriteria.list();
		}
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatMeter> meterPage = new ResultWithPageCond<IHeatMeter>();
		meterPage.setList(heatMeterList);
		meterPage.setPageCond(pageCond);
		return meterPage;
	}
	
	/**
	 * 批量设置热量表所属分摊域信息
	 * @param shareAreaUID 所属分摊域
	 * @param meterIDList 热量表ID列表，格式：XXX,XXX,XXX
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	public ResponseMsg saveHeatMeterArea(String shareAreaUID, String meterIDList) throws Exception {
		String formatMeterIDList = StringUtils.formatIDListWithSyntax(meterIDList, ",", "'");
		String updateHql = "update IHeatMeter hm " +
				"set hm.shareAreaID='"+shareAreaUID+"' " +
				"where hm.id in("+formatMeterIDList+")";
		this.runFormatHSql(updateHql);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 根据分摊域、热量表计算关系等查询热量表列表
	 * @param shareAreaUID 所属分摊域(必选)
	 * @param calculateType 热量表计算关系(必选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-4
	 */
	public List<IHeatMeter> queryHeatMetersByAreaUID(String shareAreaUID, 
			String calculateType) throws Exception {
		List<IHeatMeter> meterList = new ArrayList<IHeatMeter>();
		if(shareAreaUID!=null && !"".equals(shareAreaUID)) {
			String hsql = "from IHeatMeter hm " +
					"where hm.shareAreaID='"+shareAreaUID+"'";
			if(calculateType!=null && !"".equals(calculateType.trim())) {
				hsql += " and hm.calculateType='"+calculateType+"'";
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				hsql += " and hm.projectID in("+authorityRes+")";
			}
			hsql += " order by hm.meterNo asc,hm.meterType asc";
			meterList = this.runQueryHSql(hsql);
		}
		return meterList;
	}
	
	/**
	 * 通过热量表所属关系统计热量表的个数
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param shareAreaUID 所属分摊域(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-23
	 */
	public int countMeterSumByBelongs(String projectUID, String communityUID, 
			String buildingUID, String shareAreaUID) throws Exception {
		Criteria countCriteria = this.getCurrentSession().createCriteria(IHeatMeter.class);
		//所属项目
		if(projectUID!=null && !"".equals(projectUID.trim())) {
			countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		//所属小区
		if(communityUID!=null && !"".equals(communityUID.trim())) {
			countCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
		}
		//所属楼栋
		if(buildingUID!=null && !"".equals(buildingUID.trim())) {
			countCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
		}
		//所属分摊域
		if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
			countCriteria.add(Restrictions.eq("shareAreaID", shareAreaUID.trim()));
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
		if(!"".equals(authorityRes)) {
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
		}
		return ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
	}
	
	/**
	 * 系统热量表表号唯一性统计
	 * @param meterNo
	 * @param meterUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-8
	 */
	public int queryMeterNoUniqueCount(String meterNo, String meterUID) throws Exception {
		String hsql = "select count(*) from IHeatMeter hm " +
				"where hm.meterNo='"+meterNo+"'";
		if(meterUID!=null && !"".equals(meterUID)) {
			hsql += " and hm.id<>'"+meterUID+"'";
		}
		return ((Long)this.getCurrentSession().createQuery(hsql).uniqueResult()).intValue();
	}

	@Override
	public void saveHeatMeters(Collection heatmeters) throws Exception {
		saveEntity(heatmeters);
	}
	/**
	 * @return the iResourceAccessControlDao
	 */
	public IResourceAccessControlDao getiResourceAccessControlDao() {
		return iResourceAccessControlDao;
	}

	/**
	 * @param iResourceAccessControlDao the iResourceAccessControlDao to set
	 */
	public void setiResourceAccessControlDao(
			IResourceAccessControlDao iResourceAccessControlDao) {
		this.iResourceAccessControlDao = iResourceAccessControlDao;
	}

	@Override
	public ResultWithPageCond<IHeatMeter> queryHeatMeterPage(String projectUID,
			String communityUID, String buildingUID, String unitUID,
			String meterNo, String meterType, PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(
				IHeatMeter.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(
				IHeatMeter.class);
		// 所属项目
		if (projectUID != null && !"".equals(projectUID.trim())) {
			listCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
			countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		// 所属小区
		if (communityUID != null && !"".equals(communityUID.trim())) {
			listCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
			countCriteria.add(Restrictions.eq("communityID",communityUID.trim()));
		}
		//所属楼栋
		if(org.apache.commons.lang3.StringUtils.isNotBlank(buildingUID)){
			listCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
			countCriteria.add(Restrictions.eq("buildingID",buildingUID.trim()));
		}
		//所属单元
			if(org.apache.commons.lang3.StringUtils.isNotBlank(unitUID)){
				listCriteria.add(Restrictions.eq("unitID", unitUID.trim()));
				countCriteria.add(Restrictions.eq("unitID",unitUID.trim()));
			}
		// 热量表表号
		if (meterNo != null && !"".equals(meterNo.trim())) {
			listCriteria.add(Restrictions.ilike("meterNo", meterNo.trim()
					.toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("meterNo", meterNo.trim()
					.toLowerCase(), MatchMode.ANYWHERE));
		}
		// 热表型号
		if (meterType != null && !"".equals(meterType.trim())) {
			listCriteria.add(Restrictions.ilike("meterType", meterType.trim()
					.toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("meterType", meterType.trim()
					.toLowerCase(), MatchMode.ANYWHERE));
		}
		// 权限资源匹配
		String authorityRes = iResourceAccessControlDao
				.queryAuthorityProjResSql();
		if (!"".equals(authorityRes)) {
			// 这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in(" + authorityRes+ ")"));
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in(" + authorityRes+ ")"));
		}
		// 统计结果集个数
		int total = ((Long) countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		// 结果集排序
		listCriteria.addOrder(Order.asc("meterNo"));
		// 结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(
				pageCond.getLimit());
		// 构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatMeter> meterPage = new ResultWithPageCond<IHeatMeter>();
		meterPage.setList(listCriteria.list());
		meterPage.setPageCond(pageCond);
		return meterPage;
	}
	
	
	
	@Override
	public ArrayList<String> queryHeatMeterNos(String projectUID,
			String communityUID, String buildingUID, String unitUID,
			String meterNo, String meterType) throws Exception {
		ArrayList<String> meterNos = new ArrayList<String>();
		Criteria listCriteria = this.getCurrentSession().createCriteria(
				IHeatMeter.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(
				IHeatMeter.class);
		// 所属项目
		if (projectUID != null && !"".equals(projectUID.trim())) {
			listCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
			countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		// 所属小区
		if (communityUID != null && !"".equals(communityUID.trim())) {
			listCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
			countCriteria.add(Restrictions.eq("communityID",communityUID.trim()));
		}
		//所属楼栋
		if(org.apache.commons.lang3.StringUtils.isNotBlank(buildingUID)){
			listCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
			countCriteria.add(Restrictions.eq("buildingID",buildingUID.trim()));
		}
		//所属单元
			if(org.apache.commons.lang3.StringUtils.isNotBlank(unitUID)){
				listCriteria.add(Restrictions.eq("unitID", unitUID.trim()));
				countCriteria.add(Restrictions.eq("unitID",unitUID.trim()));
			}
		// 热量表表号
		if (meterNo != null && !"".equals(meterNo.trim())) {
			listCriteria.add(Restrictions.ilike("meterNo", meterNo.trim()
					.toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("meterNo", meterNo.trim()
					.toLowerCase(), MatchMode.ANYWHERE));
		}
		// 热表型号
		if (meterType != null && !"".equals(meterType.trim())) {
			listCriteria.add(Restrictions.ilike("meterType", meterType.trim()
					.toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("meterType", meterType.trim()
					.toLowerCase(), MatchMode.ANYWHERE));
		}
//		// 权限资源匹配
//		String authorityRes = iResourceAccessControlDao
//				.queryAuthorityProjResSql();
//		if (!"".equals(authorityRes)) {
//			// 这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
//			listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in(" + authorityRes+ ")"));
//			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in(" + authorityRes+ ")"));
//		}
//		// 统计结果集个数
//		int total = ((Long) countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		// 结果集排序
		listCriteria.addOrder(Order.asc("meterNo"));
//		// 结果集分页
//		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(
//				pageCond.getLimit());
		// 构造返回类型
//		pageCond.setTotal(total);
		List<IHeatMeter>  list = listCriteria.list();
		meterNos = (ArrayList<String>) CollectionUtils.collect(list, new Transformer() {
			@Override
			public Object transform(Object input) {
				IHeatMeter meter = (IHeatMeter) input;
				return meter.getMeterNo();
			}
		});
		return meterNos;
	}
}
